﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace OrganizeYourSearch
{
    /// <summary>
    /// Interaction logic for WinInsertTag.xaml
    /// </summary>
    public partial class WinInsertTag : Window
    {
        public Tag CurTag{get;set;}
        public TagInstanceDetail CurTagInstance{get;set;}
        public TagInstanceDetail BuildTagInstance{get;set;}
        public ObservableCollection<Tag> Tags { get; set; }

        public WinInsertTag()
        {
            InitializeComponent();
        }
        public WinInsertTag(string selDocTxt,string title, string locPath, long startPt, long endPt)
        {
            BuildTagInstance = new TagInstanceDetail(0, 0, startPt, endPt, locPath, title, selDocTxt);
            try
            {
                InitializeComponent();
                Tags = new ObservableCollection<Tag>();
                CurTag = new Tag();
                
                
                Tags = WPFApplication.Database.GetTags(SearchBox.Text);
                LstTags.ItemsSource = Tags;
                LstTags.DisplayMemberPath = "TagName";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LstTags_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CurTag = LstTags.SelectedValue as Tag;
            BuildTagInstance.TagId = CurTag.TagId;
            try
            {   
                WPFApplication.Database.InsertIntoTagInstanceDetails(BuildTagInstance);
                AddCurItemToWpInsertedTags();
            }
            catch (Exception ex)
            {
                MessageBox.Show("From WinInsertTag file, LstTags_Selection Changed" + ex.Message);
            }
            SearchBox.Text = "";
        }

        private void AddCurItemToWpInsertedTags()
        {
            try
            {
                TextBlock tblk = new TextBlock();
                tblk.Text = CurTag.TagName;
                wpInsertedTags.Children.Add(tblk);
            }
            catch (Exception ex)
            {
                MessageBox.Show("From AddCurItemToWpInsertedTags() \n" + ex.Message);
            }
        }

        private void btnDoneTagging_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnTag_Click(object sender, RoutedEventArgs e)
        {
            //CurTag = LstTags.SelectedValue as Tag;
            /*
            BuildTagInstance.TagId = CurTag.TagId;
            try
            {
                WPFApplication.Database.InsertIntoTags(SearchBox.Text);
                long id = WPFApplication.Database.SelectTagId(SearchBox.Text);
                BuildTagInstance.TagId = id;
                WPFApplication.Database.InsertIntoTagInstanceDetails(BuildTagInstance);
                AddCurItemToWpInsertedTags();
            }
            catch (Exception ex)
            {
                MessageBox.Show("From WinInsertTag file, LstTags_Selection Changed" + ex.Message);
            }
            SearchBox.Text = "";
             * */
            MessageBox.Show("Module not implemented yet.\nOnly pre-existing tag words can be reused. \nSelect any of the tag from the list below and click \"done Tagging\" \nYour document would be tagged with the corresponding tag word.");
        }

        private void tbSearchBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                Tags.Clear();
                ObservableCollection<Tag> tempTags = WPFApplication.Database.GetTags(SearchBox.Text);
                foreach (Tag tempTag in tempTags)
                {
                    Tags.Add(tempTag);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("From tbSearchBox_TextChanged\n" + ex.Message);
            }
        }
    }
}
